package com.zyf.lj.service.impl;

import com.zyf.lj.entity.College;
import com.zyf.lj.entity.Major;
import com.zyf.lj.mapper.CollegeMapper;
import com.zyf.lj.mapper.MajorMapper;
import com.zyf.lj.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学院Service实现类
 */
@Service
public class CollegeServiceImpl implements CollegeService {
    
    @Autowired
    private CollegeMapper collegeMapper;
    
    @Autowired
    private MajorMapper majorMapper;
    
    @Override
    public List<College> getAllColleges() {
        return collegeMapper.findAllColleges();
    }
    
    @Override
    public College getCollegeById(Integer id) {
        return collegeMapper.findCollegeById(id);
    }
    
    @Override
    public List<College> getAllCollegesWithMajors() {
        // 方法一：使用MyBatis的嵌套结果映射
        List<College> colleges = collegeMapper.findAllCollegesWithMajors();
        
        // 如果上面的方法有问题，可以尝试下面的手动组装方式
        if (colleges == null || colleges.isEmpty() || (colleges.size() > 0 && (colleges.get(0).getMajors() == null || colleges.get(0).getMajors().isEmpty()))) {
            // 获取所有学院
            colleges = collegeMapper.findAllColleges();
            
            // 为每个学院获取专业
            for (College college : colleges) {
                List<Major> majors = majorMapper.findMajorsByCollegeId(college.getId());
                college.setMajors(majors);
            }
        }
        
        return colleges;
    }
    
    @Override
    public College getCollegeWithMajorsById(Integer id) {
        // 方法一：使用MyBatis的嵌套结果映射
        College college = collegeMapper.findCollegeWithMajorsById(id);
        
        // 如果上面的方法有问题，可以尝试下面的手动组装方式
        if (college == null || college.getMajors() == null || college.getMajors().isEmpty()) {
            college = collegeMapper.findCollegeById(id);
            if (college != null) {
                List<Major> majors = majorMapper.findMajorsByCollegeId(id);
                college.setMajors(majors);
            }
        }
        
        return college;
    }
} 